﻿using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Md.Api.DDD;
/// <summary>
/// 工具帮助类
/// </summary>
public class UtilHelp
{
    /// <summary>
    /// 字符串转首字母大写
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>string</returns>
    public static string StrConvertToTitleCase(string str)
    {
        string result = "";

        TextInfo txtInfo = new CultureInfo("en-US", false).TextInfo;

        result = txtInfo.ToTitleCase(str);

        return result;
    }
    /// <summary>
    /// 将下划线分隔字符串，转换为小驼峰
    /// </summary>
    /// <param name="str">需要转的下划线分隔的字符串</param>
    /// <returns>string小驼峰</returns>
    public static string UnderlineToSmallHump(string str)
    {
        var list = str.ToLower().Split('_');

        var res = list[0] + string.Join("", list.Where(w => w != list[0]).Select(s => Regex.Replace(s, "^[a-z]", m => m.Value.ToUpper())));

        return res;
    }
    /// <summary>
    /// 将下划线分隔字符串，转换为大驼峰
    /// </summary>
    /// <param name="str">需要转的下划线分隔的字符串</param>
    /// <returns>string大驼峰</returns>
    public static string UnderlineToBigHump(string str)
    {
        var list = str.ToLower().Split('_');

        var res = string.Join("", list.Select(s => Regex.Replace(s, "^[a-z]", m => m.Value.ToUpper())));

        return res;
    }
    /// <summary>
    /// 获取表字段类型
    /// </summary>
    /// <param name="dbType">表字段类型</param>
    /// <returns>string</returns>
    public static string GetCLRType(string dbType)
    {
        string result = "";

        switch (dbType)
        {
            case "bigint":
                result = "long";
                break;
            case "tinyint":
            case "smallint":
            case "mediumint":
            case "int":
            case "integer":
                result = "int";
                break;
            case "double":
                result = "double";
                break;
            case "float":
                result = "float";
                break;
            case "decimal":
                result = "decimal";
                break;
            case "numeric":
            case "real":
                result = "decimal";
                break;
            case "bit":
                result = "bool";
                break;
            case "date":
            case "time":
            case "year":
            case "datetime":
            case "timestamp":
                result = "DateTime";
                break;
            case "tinyblob":
            case "blob":
            case "mediumblob":
            case "longblob":
            case "binary":
            case "varbinary":
                result = "byte[]?";
                break;
            case "char":
            case "varchar":
            case "tinytext":
            case "text":
            case "mediumtext":
            case "longtext":
                result = "string?";
                break;
            case "point":
            case "linestring":
            case "polygon":
            case "geometry":
            case "multipoint":
            case "multilinestring":
            case "multipolygon":
            case "geometrycollection":
            case "enum":
            case "set":
            default:
                result = dbType;
                break;
        }

        return result;
    }
    /// <summary>
    /// MD532位大写，数据加密
    /// </summary>
    /// <param name="value">需要加密的字符串</param>
    /// <returns>string</returns>
    public static string MD5Encrypt32(string? value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return string.Empty;
        }

        using (var md5 = MD5.Create())
        {
            var result = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
            var strResult = BitConverter.ToString(result);
            string res = strResult.Replace("-", "");
            return res;
        }
    }
}

